//
//  trackball.h
//  TreeViewerIOS
//
//  Created by Billy Clack on 10/13/12.
//  Copyright (c) 2012 Billy Clack. All rights reserved.
//

#ifndef TreeViewerIOS_trackball_h
#define TreeViewerIOS_trackball_h

#include <glm/glm.hpp>
#include <glm/ext.hpp>


class TrackBall{
public:
    TrackBall(unsigned int w, unsigned int h);

    TrackBall();
	//TrackBall( glm::vec3 & center, float rad, float inScreenW, float inScreenH );
    //Gets the rotate from the new positions based on the old ones.
	
    void UpdatePoint( unsigned int sx, unsigned int sy );
    void SetPoint(unsigned int sx, unsigned int sy);
	glm::quat GetCurrentRotation();
	void GetCurrentRotation( float * mat);
    void SetScreenSpace( unsigned int sw, unsigned int sh){
        screenW = (float)sw;
        screenH = (float)sh;
    }
    
    void Reset() { mRot = glm::quat(); }
    
private:

	void InitTrackBall(unsigned int w, unsigned int h);
    
    //Normalized
    glm::quat GetRotation( const glm::vec3 & inPoint);
    glm::vec3 GetPointFromScreenPos( float sx, float sy );
    
    //Current rotation.
    glm::quat mRot;
    
    glm::vec3 lastP;
    glm::vec3 c;
    float r;
    float rsq;
    float screenW;
    float screenH;
};

#endif
